﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Popups;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

using FindAnything.OpenAPI;

namespace FindAnything
{
    public sealed partial class main : Page
    {
        String selected_line = "";
        String selected_item = "";
        String selected_Sdate = "";
        String selected_Edate = "";

        public main()
        {
            this.InitializeComponent();
            
            Line.Items.Clear();
            Line.Items.Add("서울 1~4호선");
            Line.Items.Add("서울 5~8호선");
            Line.Items.Add("서울 9호선");
            Line.Items.Add("서울 전노선");
            Line.Items.Add("대전 지하철");

            Item.Items.Clear();
            Item.Items.Add("파일");
            Item.Items.Add("책");
            Item.Items.Add("배낭");
            Item.Items.Add("쇼핑백");
            Item.Items.Add("의류");
            Item.Items.Add("가방");
            Item.Items.Add("서류");
            Item.Items.Add("전자제품");
            Item.Items.Add("지갑");
            Item.Items.Add("기타");
            Item.Items.Add("모두");

            for (int i = 0; i < 8; i++)
            {
                End_date.Items.Add(DateTime.Now.AddDays(-(i)).ToString("yyyy-MM-dd"));
            }
            End_date.SelectedIndex = 0;
        }

        /// <summary>
        /// 이 페이지가 프레임에 표시되려고 할 때 호출됩니다.
        /// </summary>
        /// <param name="e">페이지에 도달한 방법을 설명하는 이벤트 데이터입니다. Parameter
        /// 속성은 일반적으로 페이지를 구성하는 데 사용됩니다.</param>
        /// 

        private void Line_Selection(object sender, SelectionChangedEventArgs e)
        {
            if(Line.SelectedItem.ToString().Equals("서울 1~4호선"))
            {
                this.selected_line = "s14";
            }
            else if(Line.SelectedItem.ToString().Equals("서울 5~8호선"))
            {
                this.selected_line = "s58";
            }
            else if (Line.SelectedItem.ToString().Equals("서울 9호선"))
            {
                this.selected_line = "s9";
            }
            else if (Line.SelectedItem.ToString().Equals("서울 전노선"))
            {
                this.selected_line = "seoul";
            }
            else if (Line.SelectedItem.ToString().Equals("대전 지하철"))
            {
                this.selected_line = "dj1";
            }
        }

        private void Item_Selection(object sender, SelectionChangedEventArgs e)
        {
            if(Item.SelectedItem.ToString().Equals("파일"))
            {
                this.selected_item = "900";
            }
            else if(Item.SelectedItem.ToString().Equals("책"))
            {
                this.selected_item = "800";
            }
            else if(Item.SelectedItem.ToString().Equals("배낭"))
            {
                this.selected_item = "500";
            }
            else if(Item.SelectedItem.ToString().Equals("쇼핑백"))
            {
                this.selected_item = "200";
            }
            else if(Item.SelectedItem.ToString().Equals("옷"))
            {
                this.selected_item = "700";
            }
            else if(Item.SelectedItem.ToString().Equals("가방"))
            {
                this.selected_item = "400";
            }
            else if(Item.SelectedItem.ToString().Equals("서류"))
            {
                this.selected_item = "300";
            }
            else if(Item.SelectedItem.ToString().Equals("전자제품"))
            {
                this.selected_item = "600";
            }
            else if(Item.SelectedItem.ToString().Equals("지갑"))
            {
                this.selected_item = "100";
            }
            else if(Item.SelectedItem.ToString().Equals("기타"))
            {
                this.selected_item = "0";
            }
            else if(Item.SelectedItem.ToString().Equals("모두"))
            {
                this.selected_item = "all";
            }
        }

        private async void Search(object sender, RoutedEventArgs e)
        {
            if (this.selected_item.Equals(""))
            {
                this.selected_item = "all";
            }
            if (this.selected_Edate.Equals(""))
            {
                this.selected_Edate = DateTime.Now.ToString("yyyy-MM-dd");
            }
            if (this.selected_Sdate.Equals(""))
            {
                this.selected_Sdate = DateTime.Parse(this.selected_Edate).AddDays(-30).ToString("yyyy-MM-dd");
            }

            String search_str = this.selected_line + "," + this.selected_Sdate.Replace("-", "") + "," + this.selected_Edate.Replace("-", "") + "," + this.selected_item;

            if (selected_line.Equals(""))
            {
                MessageDialog msg = new Windows.UI.Popups.MessageDialog("호선을 입력해 주세요");
                await msg.ShowAsync();
            }
            else
            {
                this.Frame.Navigate(typeof(LostList), search_str);
            }
        }

        private void Sdate_Selection(object sender, SelectionChangedEventArgs e)
        {
            if (Start_date.SelectedItem == null )
            {
                this.selected_Sdate = "";
            }
            else
            {
                this.selected_Sdate = Start_date.SelectedItem.ToString();
            }
        }

        private void Edate_Selection(object sender, SelectionChangedEventArgs e)
        {
                this.selected_Edate = End_date.SelectedItem.ToString();

                Start_date.Items.Clear();
                
                for (int i = 1; i < 31; i++)
                {
                    Start_date.Items.Add(DateTime.Parse(this.selected_Edate).AddDays(-(i)).ToString("yyyy-MM-dd"));
                }
                
        }

    }
}
